using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using OpenComposite.Base.Designer;
using WeifenLuo.WinFormsUI.Docking;
using System.ComponentModel.Design;

namespace OpenComposite.EII.Designer
{
	public partial class DesignerViewDockContent : DockContent
	{
		#region Initialization
		public DesignerViewDockContent(IController controller)
		{
			_controller = controller;
			_controller.Loaded += new EventHandler(_controller_Loaded);

			InitializeComponent();
			initDesignerViewControl(new DesignerViewControl());
			init();
		}
		public DesignerViewDockContent(IController controller, Control designerView)
		{
			_controller = controller;
			_controller.Loaded += new EventHandler(_controller_Loaded);

			InitializeComponent();
			initDesignerViewControl(designerView);
			init();
		}

		private void _controller_Loaded(object sender, EventArgs e)
		{
			addVerbsToToolStrip();
		}
		private void init()
		{
			if ( this.DesignMode )
				return;

			this.ShowHint = DockState.Document;

			addVerbsToToolStrip();
		}

		private void addVerbsToToolStrip()
		{
			for ( int i = 3; i < toolStrip1.Items.Count; i++ ) {
				toolStrip1.Items.RemoveAt(3);
			}
			if ( _controller.Verbs != null && _controller.Verbs.Count > 0 ) {
				this.ToolStrip.Items.Add(new ToolStripSeparator());
				foreach ( DesignerVerb dv in _controller.Verbs ) {
					if ( dv.Supported ) {
						ToolStripButton tsbtn = new ToolStripButton();
						tsbtn.Text = dv.Text;
						tsbtn.ToolTipText = dv.Description;
						tsbtn.Checked = dv.Checked;
						tsbtn.Enabled = dv.Enabled;
						tsbtn.Visible = dv.Visible;
						if ( dv.Properties.Contains("Image") ) {
							tsbtn.Image = dv.Properties["Image"] as Image;
							tsbtn.TextImageRelation = TextImageRelation.ImageBeforeText;
						}
						if ( dv.Properties.Contains("ImageTransparentColor") ) {
							tsbtn.ImageTransparentColor = (Color)dv.Properties["ImageTransparentColor"];
						}
						tsbtn.Tag = dv;
						tsbtn.Click += new EventHandler(this.onToolStripVerbClick);
						this.ToolStrip.Items.Add(tsbtn);
					}
				}
			}
		}
		#endregion

		#region Properties
		// public
		public DesignerViewControl DesignerView
		{
			get { return _ctrlDesigner; }
		}
		public DesignerViewPanel DesignerPanel
		{
			get { return _ctrlDesigner.MainPanel; }
		}
		public ToolStrip ToolStrip
		{
			get { return toolStrip1; }
		}
		public IController Controller
		{
			get { return _controller; }
		}
		#endregion

		#region Methods
		private void initDesignerViewControl(Control ctrlDesignerView)
		{
			this._ctrlDesigner = ctrlDesignerView as OpenComposite.Base.Designer.DesignerViewControl;
			this._ctrlView = ctrlDesignerView;
			// 
			// _ctrlView
			// 
			this._ctrlView.Dock = System.Windows.Forms.DockStyle.Fill;
			this._ctrlView.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ( (byte)( 0 ) ));
			this._ctrlView.Location = new System.Drawing.Point(0, 25);
			this._ctrlView.Name = "_ctrlDesigner";
			this._ctrlView.Size = new System.Drawing.Size(798, 552);
			this._ctrlView.TabIndex = 0;
			if ( _ctrlDesigner != null ) {
				this._ctrlDesigner.GridSize = new System.Drawing.Size(10, 10);
				this._ctrlDesigner.MultiSelect = false;
			}
			this.Controls.Add(this._ctrlView);
			this._ctrlView.BringToFront();
		}
		#endregion

		#region Events
		public event EventHandler Save = null;
		public event EventHandler ShowXml = null;
		#endregion


		protected override void OnClosing(CancelEventArgs e)
		{
		}

		#region Event Handlers
		private void saveToolStripButton_Click(object sender, EventArgs e)
		{
			EventHandler tmp = this.Save;
			if ( tmp != null )
				tmp(this, e);
		}
		private void tsbtnShowXml_Click(object sender, EventArgs e)
		{
			EventHandler tmp = this.ShowXml;
			if ( tmp != null )
				tmp(this, e);
		}
		private void onToolStripVerbClick(object sender, EventArgs e)
		{
			ToolStripItem item = sender as ToolStripItem;
			if ( item == null ) return;

			MenuCommand command = item.Tag as MenuCommand;
			if ( command == null ) return;

			command.Invoke();
		}
		#endregion

		#region Fields
		private IController _controller;
		private Control _ctrlView;
		private OpenComposite.Base.Designer.DesignerViewControl _ctrlDesigner;
		#endregion

		private void closeToolStripMenuItem_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void closeAllButThisToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Helpers.CloseAllWindows(this.DockPanel, this);
		}

		private void closeAllToolStripMenuItem_Click(object sender, EventArgs e)
		{
			Helpers.CloseAllWindows(this.DockPanel);
		}

		//private void closeAll(bool butThis)
		//{
		//    if ( this.DockPanel == null || this.DockPanel.Documents == null ) return;
		//    List<IDockContent> documents = new List<IDockContent>(this.DockPanel.Documents);
		//    for ( int i = documents.Count - 1; i >= 0; i-- ) {
		//        IDockContent doc = documents[i];
		//        if ( doc is formDomainEditor ) continue;
		//        if ( doc.DockHandler == null ) continue;
		//        if ( butThis && doc == this ) continue;
		//        doc.DockHandler.Close();
		//    }
		//}
	}
}